package org.zfes.snowier.cloud.zbss.pmp.service.impl;

import java.util.Calendar;
import java.util.Map;

import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.core.exceptions.ServiceLogicalException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpCloudMemberService;
import org.zfes.snowier.cloud.zbss.pmp.service.IPmpWeeklyService;
import org.zfes.snowier.cloud.zbss.pmp.dao.PmpWeeklyMapper;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpWeeklyCreateDto;
import org.zfes.snowier.cloud.zbss.pmp.dto.PmpWeeklyUpdateDto;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpCloudMember;
import org.zfes.snowier.cloud.zbss.pmp.model.PmpWeekly;

@Service
public class PmpWeeklyServiceImpl implements IPmpWeeklyService {
	@Autowired
	private PmpWeeklyMapper pmpWeeklyMapper;
	@Autowired
	private IPmpCloudMemberService pmpCloudMemberService;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void createPmpWeekly(Long userId, PmpWeeklyCreateDto createDto) {

		ZBeanUtil.validateBean(createDto);

		// 账号ID处理
		PmpCloudMember member = pmpCloudMemberService.loadPmpCloudMemberById(userId)
				.orElseThrow(() -> new ServiceLogicalException("未查找到该用户"));
		ZAssert.equals(member.getStatus(), Byte.valueOf("1"), "该用户状态错误");

		PmpWeekly pmpWeekly = new PmpWeekly();
		ZBeanUtil.copy(createDto, pmpWeekly, true);

		pmpWeekly.setCreatorUserId(userId).setCreatorUserName(member.getUserName()).setUserType(member.getUserType())
				.setCreatetime(ZDateUtil.newDate()).setUpdatetime(ZDateUtil.newDate()).setStatus(Byte.valueOf("0"));
		// 所属年份 所属周
		pmpWeekly.setWeeklyYear(ZDateUtil.getYear(ZDateUtil.newDate()));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(ZDateUtil.newDate());
		pmpWeekly.setWeekNum(calendar.get(Calendar.WEEK_OF_YEAR));

		pmpWeeklyMapper.insertSelective(pmpWeekly);
	}

	@Transactional(rollbackFor = Exception.class, readOnly = true)
	@Override
	public Optional<PmpWeekly> loadPmpWeeklyById(Long id) {
		if (id != null) {
			return Optional.ofNullable(pmpWeeklyMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();

	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updatePmpWeeklyByDev(Long id, PmpWeeklyUpdateDto updateDto) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZBeanUtil.validateBean(updateDto);
		
		PmpWeekly pmpWeekly = loadPmpWeeklyById(id).orElseThrow(()->new ServiceLogicalException("未找到该周报"));
		ZAssert.equals(pmpWeekly.getStatus(), Byte.valueOf("0"), "周报当前状态不允许修改内容");
		
		ZBeanUtil.copy(updateDto, pmpWeekly, true);
		
		pmpWeekly.setUpdatetime(ZDateUtil.newDate());
		pmpWeeklyMapper.updateByPrimaryKeySelective(pmpWeekly);
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void deletePmpWeekly(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		loadPmpWeeklyById(id).orElseThrow(()->new ServiceLogicalException("未找到该周报"));
		pmpWeeklyMapper.deleteByPrimaryKey(id);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void submitPmpWeekly(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");

		PmpWeekly pmpWeekly = loadPmpWeeklyById(id).orElseThrow(()->new ServiceLogicalException("未找到该周报"));
		ZAssert.equals(pmpWeekly.getStatus(), Byte.valueOf("0"), "该周报已提交");
		
		pmpWeekly.setStatus(Byte.valueOf("1")).setUpdatetime(ZDateUtil.newDate());
		
		pmpWeeklyMapper.updateByPrimaryKeySelective(pmpWeekly);
		
	}
	@Transactional(rollbackFor = Exception.class, readOnly = true)
	@Override
	public DataSet loadPmpWeeklyDataSet(Map<String, Object> params) {
		ParamMap pm = ParamMap.filterParam(params);
		DataSet ds = DataSet.newDS2(pmpWeeklyMapper.selectListCount(pm), pmpWeeklyMapper.selectMapListPage(pm));
		return ds;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void updatePmpWeeklyByPm(long id, String pmComment) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(pmComment, "请填写项目经理评价");
		ZAssert.notEmpty(pmComment, "请填写项目经理评价");

		PmpWeekly pmpWeekly = loadPmpWeeklyById(id).orElseThrow(() -> new ServiceLogicalException("未找到该数据"));
		ZAssert.notEquals(pmpWeekly, Byte.valueOf("0"), "该周报尚未提交，不可评阅");
		
		pmpWeekly.setPmComment(pmComment);
		pmpWeekly.setUpdateTime(ZDateUtil.newDate());
		pmpWeekly.setStatus(Byte.valueOf("2"));
		
		pmpWeeklyMapper.update(pmpWeekly);
	}

}